<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/24
 * Time: 17:43
 */

echo date("Ymd H:i:s") . PHP_EOL;
$urls = [
    'http://www.baidu.com',
    'http://www.qq.com',
    'http://www.163.com',
    'http://www.baidu.com?search=qq',
    'http://www.baidu.com?search=kk',
    'http://www.baidu.com?search=66',
    'http://www.baidu.com?search=99',
];
/**
 * 传统并行，有几个url，就是执行多少s

foreach($urls as $u){
    //todo 1s
    //$content = file_get_contents($u);
    sleep(1);
}*/
$count = count($urls);
for($i=0;$i<$count;$i++){
    //开启子进程
    $process = new swoole_process(function(swoole_process $pro) use ($i, $urls){
        //模拟curl请求
        $content = curlData($urls[$i]);
        //echo $content . PHP_EOL;
        $pro->write($content . PHP_EOL);
    }, true);
    $pid = $process->start();
    $workers[$i] = $process;
}

foreach($workers as $pro){
    echo $pro->read();
}

/**
 * @todo 模拟请求url
*/
function curlData($url){
    //模拟请求url
    sleep(1);
    return $url;
}
echo date("Ymd H:i:s") . PHP_EOL;